//
//  ListenViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15-10-23.
//  Copyright (c) 2015年 com.lanou3g. All rights reserved.
//

#import "ListenViewController.h"
#import "RecListenElseTableViewCell.h"
#import "RecListenTableViewCell.h"
#import "RecListenModel.h"
#import "RecListenDetailsViewController.h"
#import "RecCityViewController.h"
#import "Reachability.h"
@interface ListenViewController ()<UITableViewDataSource, UITableViewDelegate,recCityDelegate>

@end

@implementation ListenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    _dataArr = [[NSMutableArray alloc] init];
    
    //设置返回按钮的颜色及文字
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = barButtonItem;
    
    [self setTitle:@"随心听"];
    if ([self haveOrNot] == NO) {
        
        //[self getNextData];
    }else{
        
        [self getData:@"59"];
    }
    self.view.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
}

-(void)setUpPage
{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 114) style:UITableViewStylePlain];
    _table.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    _table.delegate = self;
    _table.dataSource = self;
    [self.view addSubview:_table];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10 * WIDTH, 5 * HEIGHT, 30 * WIDTH, 30 * HEIGHT);
    [button setBackgroundImage:[UIImage imageNamed:@"city.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(presentToCity) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
}
-(void)presentToCity
{
    RecCityViewController *city = [[RecCityViewController alloc] init];
    city.delegate = self;
    [self presentViewController:city animated:YES completion:nil];
}
-(void)giveCity:(RecCityModel *)cityModel
{
    [_dataArr removeAllObjects];
    [_table reloadData];
    [self getData:cityModel.myId];
   
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 200 * HEIGHT;
    }else{
        return 120 * HEIGHT;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecListenModel *model = [_dataArr objectAtIndex:indexPath.row];
    if (indexPath.row == 0) {
        static NSString *cellID = @"cell";
        RecListenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[RecListenTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        [cell getData:model];
        return cell;
    }else{
        static NSString *elseCellID = @"elseCell";
        RecListenElseTableViewCell *elseCell = [tableView dequeueReusableCellWithIdentifier:elseCellID];
        if (!elseCell) {
            elseCell = [[RecListenElseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:elseCellID];
        }
        elseCell.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
        elseCell.selectionStyle = UITableViewCellSelectionStyleNone;
        [elseCell getData:model];
        return elseCell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecListenModel *model = [_dataArr objectAtIndex:indexPath.row];
    RecListenDetailsViewController *listen = [[RecListenDetailsViewController alloc] init];
    [self.navigationController pushViewController:listen animated:YES];
    listen.model = model;
    
}
-(void)getData:(NSString *)cityId;
{
    [Refresh getdataWithString:[NSString stringWithFormat:@"http://webapi.yilule.com:5580/api/Listen?dataId=%@&pageIndex=1&pageSize=20",cityId] block:^(id block) {
       
//        for (NSDictionary *dic in [[block objectForKey:@"Data"]objectForKey:@"List"]) {
//            RecListenModel *model = [RecListenModel modelWithDictionary:dic];
//            [_dataArr addObject:model];
//        }
        [self setUpPage];
//        [_table reloadData];
        [self getMyData:block];
        [_table reloadData];
    }];
}
-(void)getNextData
{
    NSURLCache *cache = [[NSURLCache alloc] init];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://webapi.yilule.com:5580/api/Listen?dataId=59&pageIndex=1&pageSize=20"]];
    //获取缓存
    NSCachedURLResponse *response = [cache cachedResponseForRequest:request];
    
    if (response.data != nil) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:response.data options:NSJSONReadingMutableContainers error:nil];
        
        [self getMyData:dic];
    }
    [self setUpPage];
    [_table reloadData];
}
-(void)getMyData:(NSDictionary *)myDic
{
    for (NSDictionary *dic in [[myDic objectForKey:@"Data"]objectForKey:@"List"]) {
        RecListenModel *model = [RecListenModel modelWithDictionary:dic];
        [_dataArr addObject:model];
    }
}
#pragma mark - 判断网络状态
-(BOOL)haveOrNot
{
    Reachability *reach = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    switch ([reach currentReachabilityStatus]) {
        case NotReachable:
            return NO;
            break;
        case ReachableViaWWAN:
            return YES;
            break;
        case ReachableViaWiFi:
            return YES;
            break;
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
